const ajax = require('../../tool/ajax');
const screen = require('./screen');//截图

/**
 * 签章预览脚本
 * @param page 框架页面
 */
const start = async (page) => {
  //当前页面的URL
  let url = page.target()._targetInfo.url;
  //某些签章预览页面调用的接口不一样，需要判断
  if (url.indexOf('tribunal/courtNotice?caseId=') >= 0) {
    await ajax.waitForResponse(page, 'createTribunalNotice.do');
  } else {
    await ajax.waitForResponse(page, 'getFileInfo.do');
  }

  //获取三种UI下的签章状态
  let status = await page.$eval('div.ant-card-head-title', e => e.innerHTML).then(
    (result) => result,
    (e) => page.$eval('span.body-head-text', e => e.innerHTML).then(
      (result) => result,
      (e) => page.$eval('h3.notice_info_title', e => e.innerHTML)
    )
  );

  await page.click('button.ant-btn');

  //如果没有签章，就需要输入密码进行签章
  if (status.indexOf('待签章') >= 0 || status.indexOf('待签名') >= 0) {
    //等待弹框加载
    await page.waitFor(1000);
    await page.type('.ant-modal-body input', '123456');
    await page.click('.ant-modal-content button.ant-btn-primary');
    //等待弹框隐藏
    await page.waitFor('.ant-modal-body input', {hidden: true});

    await screen(page,'电子签章');

    await page.click('button.ant-btn');
  }
}

module.exports = start;